package com.cy.myblog.util;

import com.cy.myblog.pageModel.MailInfoDTO;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Date;
import java.util.Properties;

/**
 * 邮件工具类
 * Created by cy on 2015/6/18.
 */
public class MailUtil {

    /**
     * 简单的邮件发送器
     * @param mailInfo
     * @return
     */
    public static boolean sendEmail(MailInfoDTO mailInfo){
        Session session = getSession(mailInfo);


        try{
            for(int i=0;i < mailInfo.getToAddress().length;i++){
            // 根据session创建一个邮件消息
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(mailInfo.getUsername()));
            msg.setRecipient(Message.RecipientType.TO, new InternetAddress(mailInfo.getToAddress()[i]));
            msg.setSubject(mailInfo.getSubject());
            msg.setSentDate(new Date());
            msg.setText(mailInfo.getContent());
            Transport.send(msg); //发送邮件
            }
        }catch(Exception ex){
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    public static boolean sendHtmlMail(MailInfoDTO mailInfo){
        Session session = getSession(mailInfo);
        try{
            for(int i=0;i<mailInfo.getToAddress().length;i++) {
                // 根据session创建一个邮件消息
                Message msg = new MimeMessage(session);
                msg.setFrom(new InternetAddress(mailInfo.getUsername()));
                msg.setRecipient(Message.RecipientType.TO, new InternetAddress(mailInfo.getToAddress()[i]));
                msg.setSubject(mailInfo.getSubject());
                msg.setSentDate(new Date());

                // Multipart类是一个容器类,包含MimeBodyPart类型的对象
                Multipart mainPart = new MimeMultipart();
                // 创建一个包含HTML内容的MimeBodyPart
                BodyPart html = new MimeBodyPart();
                // 设置HTML内容
                html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
                mainPart.addBodyPart(html);
                // 将Multipart对象设置为邮件内容
                msg.setContent(mainPart);

                Transport.send(msg); //发送邮件
            }
        }catch(Exception ex){
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    private static Session getSession(MailInfoDTO mailInfo) {
        Properties props = new Properties();
        // 设置邮件会话属性
        props.put("mail.smtp.host", SMTPUtil.autoMailServer(mailInfo.getUsername()));
        props.put("mail.smtp.port","25");
        props.setProperty("mail.smtp.auth",mailInfo.isValidate() ? "true" : "false");

        MyAuthenticator authen = null;

        // 如果需要身份验证,则创建一个密码验证器
        if(mailInfo.isValidate())
            authen = new MyAuthenticator(mailInfo.getUsername(), mailInfo.getPassword());

        // 根据邮件会话属性和密码验证器构造一个发送邮件的session
        return Session.getInstance(props, authen);
    }

}
